<?php

App::uses('Security', 'Utility');

class AuthresourcegroupsController extends AppController {

    var $helpers = array('Html', 'Form');
    var $uses = array('Authresourcegroup', 'Authpermission');
    var $paginate = array('limit' => '10');

    function index() {
        $this->set('menu', 'users');
        $this->set('submenu', 'resourcegroups');
        $this->set('cakeDescription', 'Resource Groups');
        $this->set('title_for_layout', 'Index');
        $textsearch = "";
        if (!empty($this->data)) {
            $textsearch = $this->data['Authresourcegroup']['textsearch'];
            $this->paginate['conditions'] = array('Authresourcegroups.name LIKE' => '%' . $textsearch . '%');
        } else {
            if (isset($this->params['named']['textsearch'])) {
                $textsearch = $this->params['named']['textsearch'];
                $this->paginate['conditions'] = array('Authresourcegroup.name LIKE' => '%' . $textsearch . '%');
            }
        }        
        $this->Authresourcegroup->recursive = 0;
        $this->set('authresourcegroups', $this->paginate());
        $this->set('textsearch', $textsearch);
    }

    function view($id = null) {
        $this->set('menu', 'users');
        $this->set('submenu', 'resourcegroups');
        $this->set('cakeDescription', 'Resource Groups');
        $this->set('title_for_layout', 'View');
        if (!$id) {
            $this->Session->setFlash(__('Invalid Authresourcegroup', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('authresourcegroup', $this->Authresourcegroup->read(null, $id));
    }

    function add() {
        $this->set('menu', 'users');
        $this->set('submenu', 'resourcegroups');
        $this->set('cakeDescription', 'Resource Groups');
        $this->set('title_for_layout', 'Add');
        if (!empty($this->data)) {
            $this->Authresourcegroup->create();
            if ($this->Authresourcegroup->save($this->data)) {
                $this->Session->setFlash(__('The Authresourcegroup has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The Authresourcegroup could not be saved. Please, try again.', true));
            }
        }
        $parentresourcegroups = $this->Authresourcegroup->Parentresourcegroup->find('list');
        $this->set(compact('parentresourcegroups'));
    }

    function edit($id = null) {
        $this->set('menu', 'users');
        $this->set('submenu', 'resourcegroups');
        $this->set('cakeDescription', 'Resource Groups');
        $this->set('title_for_layout', 'Edit');
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Authresourcegroup', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if (empty($this->data['Authresourcegroup']['parent_id'])) {
                $this->data['Authresourcegroup']['parent_id'] = 'EMPTY';
            }

            if ($this->Authresourcegroup->save($this->data)) {
                $this->Session->setFlash(__('The Authresourcegroup has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The Authresourcegroup could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Authresourcegroup->read(null, $id);
        }
        $parentresourcegroups = $this->Authresourcegroup->Parentresourcegroup->find('list');
        $this->set(compact('parentresourcegroups'));
    }

    function delete($id = null) {
        Configure::write('debug', 0);
        if (!$id) {
            $this->Session->setFlash(__('Invalid Authresourcegroup', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Authresourcegroup->delete($id)) {

            $this->Authresourcegroup->recursive = -1;
            $arrResourcegroups = $this->Authresourcegroup->find('all', array('conditions' => array('parent_id' => $id)));
            if (count($arrResourcegroups) > 0) {
                $arr_data = array();
                foreach ($arrResourcegroups as $data) {

                    $arr_data = array('id' => $data['Authresourcegroup']['id'], 'parent_id' => 'EMPTY');
                    $this->Authresourcegroup->save($arr_data);
                }
            }

            $this->Session->setFlash(__('Authresourcegroup deleted', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('not deleted', true));
            $this->redirect(array('action' => 'index'));
        }
    }

}

?>